﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SMS
{
    public class TinNhan
    {
        public string[] SDTLan1;
        public string[] SDTLan2;
        public string[] SDTLan3;
        public string[] SDTLan4;
        public string[] SDTLan5;
        public string[] SDTLan6;

        public string[] TenKHLan1;
        public string[] TenKHLan2;
        public string[] TenKHLan3;
        public string[] TenKHLan4;
        public string[] TenKHLan5;
        public string[] TenKHLan6;

        public string[] SoKhungLan1;
        public string[] SoKhungLan2;
        public string[] SoKhungLan3;
        public string[] SoKhungLan4;
        public string[] SoKhungLan5;
        public string[] SoKhungLan6;

        public string[] HanCuoiLan1;
        public string[] HanCuoiLan2;
        public string[] HanCuoiLan3;
        public string[] HanCuoiLan4;
        public string[] HanCuoiLan5;
        public string[] HanCuoiLan6;

        public string[] CMNDLan1;
        public string[] CMNDLan2;
        public string[] CMNDLan3;
        public string[] CMNDLan4;
        public string[] CMNDLan5;
        public string[] CMNDLan6;

        public string TinNhanLan1;
        public string TinNhanLan2;
        public string TinNhanLan3;
        public string TinNhanLan4;
        public string TinNhanLan5;
        public string TinNhanLan6;

        fLogin main;

        public TinNhan(fLogin main1)
        {
            main = main1;
        }

        //------------------------------------------------------------------------------------
        public void Lay_All()
        {
            Lay_All_Lan1();
            Lay_All_Lan2();
            Lay_All_Lan3();
            Lay_All_Lan4();
            Lay_All_Lan5();
            Lay_All_Lan6();
            main.ThongBaoTinNhanCanGui.lblSoTNCanGui.Text = SoTinNhanCanGui().ToString();

        }
        public void Lay_All_Lan1()
        {
            string[] TempSDT = new string[10000];
            string[] TempKH = new string[10000];
            string[] TempSoKhung = new string[10000];
            string[] TempHanCuoi = new string[10000];
            string[] TempCMND = new string[10000];
            int i = 0;
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT XE.SOKHUNG, BIDANH, DTDIDONG, HANBD, NGAYMUA = DATEADD(MONTH, HANBD, NGAYMUA), NOIDUNG, KH.CMND FROM KHACHHANG KH, XEMAY XE, LOAITINNHAN LO "
                                + "WHERE XE.CMND = KH.CMND "
                                + "		AND "
                                + " KH.TINHTRANG = 'YES' "
                                + " AND "
                                + "	LO.LOAITN = @LAN1 "
                                + "		AND "
                                + "	XE.TINHTRANGBD <1 "
                                + "		AND "
                                + "	XE.TINHTRANGGUITIN <1 "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) >= (LO.NGAYBATDAUGUI) "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) <= (LO.NGAYKETTHUCGUI)";

            command.Parameters.Add(new SqlParameter("@LAN1", "Lần 1"));
            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TempSoKhung[i] = reader[0].ToString();
                    TempKH[i] = reader[1].ToString();
                    TempSDT[i] = reader[2].ToString();
                    TempHanCuoi[i] = reader[4].ToString();
                    TinNhanLan1 = reader[5].ToString();
                    TempCMND[i] = reader[6].ToString();
                    i++;
                }
                SDTLan1 = PerfectArray(TempSDT);
                TenKHLan1 = PerfectArray(TempKH);
                SoKhungLan1 = PerfectArray(TempSoKhung);
                HanCuoiLan1 = PerfectArray(TempHanCuoi);
                CMNDLan1 = PerfectArray(TempCMND);

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }
        }

        public void Lay_All_Lan2()
        {
            string[] TempSDT = new string[10000];
            string[] TempKH = new string[10000];
            string[] TempSoKhung = new string[10000];
            string[] TempHanCuoi = new string[10000];
            string[] TempCMND = new string[10000];
            int i = 0;
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT XE.SOKHUNG, BIDANH, DTDIDONG, HANBD, NGAYMUA = DATEADD(MONTH, HANBD, NGAYMUA), NOIDUNG, KH.CMND FROM KHACHHANG KH, XEMAY XE, LOAITINNHAN LO "
                                + "WHERE XE.CMND = KH.CMND "
                                + "		AND "
                                 + " KH.TINHTRANG = 'YES' "
                                + " AND "
                                + "	LO.LOAITN = @LAN2 "
                                + "		AND "
                                + "	XE.TINHTRANGBD <2 "
                                + "		AND "
                                + "	XE.TINHTRANGGUITIN <2 "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) >= (LO.NGAYBATDAUGUI) "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) <= (LO.NGAYKETTHUCGUI)";

            command.Parameters.Add(new SqlParameter("@LAN2", "Lần 2"));
            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TempSoKhung[i] = reader[0].ToString();
                    TempKH[i] = reader[1].ToString();
                    TempSDT[i] = reader[2].ToString();
                    TempHanCuoi[i] = reader[4].ToString();
                    TinNhanLan2 = reader[5].ToString();
                    TempCMND[i] = reader[6].ToString();
                    i++;
                }
                SDTLan2 = PerfectArray(TempSDT);
                TenKHLan2 = PerfectArray(TempKH);
                SoKhungLan2 = PerfectArray(TempSoKhung);
                HanCuoiLan2 = PerfectArray(TempHanCuoi);
                CMNDLan2 = PerfectArray(TempCMND);

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }

        }

        public void Lay_All_Lan3()
        {
            string[] TempSDT = new string[10000];
            string[] TempKH = new string[10000];
            string[] TempSoKhung = new string[10000];
            string[] TempHanCuoi = new string[10000];
            string[] TempCMND = new string[10000];
            int i = 0;
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT XE.SOKHUNG, BIDANH, DTDIDONG, HANBD, NGAYMUA = DATEADD(MONTH, HANBD, NGAYMUA), NOIDUNG, KH.CMND FROM KHACHHANG KH, XEMAY XE, LOAITINNHAN LO "
                                + "WHERE XE.CMND = KH.CMND "
                                + "		AND "
                                 + " KH.TINHTRANG = 'YES' "
                                + " AND "
                                + "	LO.LOAITN = @LAN3 "
                                + "		AND "
                                + "	XE.TINHTRANGBD <3 "
                                + "		AND "
                                + "	XE.TINHTRANGGUITIN <3 "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) >= (LO.NGAYBATDAUGUI) "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) <= (LO.NGAYKETTHUCGUI)";

            command.Parameters.Add(new SqlParameter("@LAN3", "Lần 3"));
            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TempSoKhung[i] = reader[0].ToString();
                    TempKH[i] = reader[1].ToString();
                    TempSDT[i] = reader[2].ToString();
                    TempHanCuoi[i] = reader[4].ToString();
                    TinNhanLan3 = reader[5].ToString();
                    TempCMND[i] = reader[6].ToString();
                    i++;
                }
                SDTLan3 = PerfectArray(TempSDT);
                TenKHLan3 = PerfectArray(TempKH);
                SoKhungLan3 = PerfectArray(TempSoKhung);
                HanCuoiLan3 = PerfectArray(TempHanCuoi);
                CMNDLan3 = PerfectArray(TempCMND);

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }

        }

        public void Lay_All_Lan4()
        {
            string[] TempSDT = new string[10000];
            string[] TempKH = new string[10000];
            string[] TempSoKhung = new string[10000];
            string[] TempHanCuoi = new string[10000];
            string[] TempCMND = new string[10000];
            int i = 0;
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT XE.SOKHUNG, BIDANH, DTDIDONG, HANBD, NGAYMUA = DATEADD(MONTH, HANBD, NGAYMUA), NOIDUNG, KH.CMND FROM KHACHHANG KH, XEMAY XE, LOAITINNHAN LO "
                                + "WHERE XE.CMND = KH.CMND "
                                + "		AND "
                                 + " KH.TINHTRANG = 'YES' "
                                + " AND "
                                + "	LO.LOAITN = @LAN4 "
                                + "		AND "
                                + "	XE.TINHTRANGBD <4 "
                                + "		AND "
                                + "	XE.TINHTRANGGUITIN <4 "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) >= (LO.NGAYBATDAUGUI) "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) <= (LO.NGAYKETTHUCGUI)";

            command.Parameters.Add(new SqlParameter("@LAN4", "Lần 4"));
            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TempSoKhung[i] = reader[0].ToString();
                    TempKH[i] = reader[1].ToString();
                    TempSDT[i] = reader[2].ToString();
                    TempHanCuoi[i] = reader[4].ToString();
                    TinNhanLan4 = reader[5].ToString();
                    TempCMND[i] = reader[6].ToString();
                    i++;
                }
                SDTLan4 = PerfectArray(TempSDT);
                TenKHLan4 = PerfectArray(TempKH);
                SoKhungLan4 = PerfectArray(TempSoKhung);
                HanCuoiLan4 = PerfectArray(TempHanCuoi);
                CMNDLan4 = PerfectArray(TempCMND);

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }
        }

        public void Lay_All_Lan5()
        {
            string[] TempSDT = new string[10000];
            string[] TempKH = new string[10000];
            string[] TempSoKhung = new string[10000];
            string[] TempHanCuoi = new string[10000];
            string[] TempCMND = new string[10000];
            int i = 0;
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT XE.SOKHUNG, BIDANH, DTDIDONG, HANBD, NGAYMUA = DATEADD(MONTH, HANBD, NGAYMUA), NOIDUNG, KH.CMND FROM KHACHHANG KH, XEMAY XE, LOAITINNHAN LO "
                                + "WHERE XE.CMND = KH.CMND "
                                + "		AND "
                                 + " KH.TINHTRANG = 'YES' "
                                + " AND "
                                + "	LO.LOAITN = @LAN5 "
                                + "		AND "
                                + "	XE.TINHTRANGBD <5 "
                                + "		AND "
                                + "	XE.TINHTRANGGUITIN <5 "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) >= (LO.NGAYBATDAUGUI) "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) <= (LO.NGAYKETTHUCGUI)";

            command.Parameters.Add(new SqlParameter("@LAN5", "Lần 5"));
            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TempSoKhung[i] = reader[0].ToString();
                    TempKH[i] = reader[1].ToString();
                    TempSDT[i] = reader[2].ToString();
                    TempHanCuoi[i] = reader[4].ToString();
                    TinNhanLan5 = reader[5].ToString();
                    TempCMND[i] = reader[6].ToString();
                    i++;
                }
                SDTLan5 = PerfectArray(TempSDT);
                TenKHLan5 = PerfectArray(TempKH);
                SoKhungLan5 = PerfectArray(TempSoKhung);
                HanCuoiLan5 = PerfectArray(TempHanCuoi);
                CMNDLan5 = PerfectArray(TempCMND);

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }
        }

        public void Lay_All_Lan6()
        {
            string[] TempSDT = new string[10000];
            string[] TempKH = new string[10000];
            string[] TempSoKhung = new string[10000];
            string[] TempHanCuoi = new string[10000];
            string[] TempCMND = new string[10000];
            int i = 0;
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "SELECT XE.SOKHUNG, BIDANH, DTDIDONG, HANBD, NGAYMUA = DATEADD(MONTH, HANBD, NGAYMUA), NOIDUNG, KH.CMND FROM KHACHHANG KH, XEMAY XE, LOAITINNHAN LO "
                                + "WHERE XE.CMND = KH.CMND "
                                + "		AND "
                                 + " KH.TINHTRANG = 'YES' "
                                + " AND "
                                + "	LO.LOAITN = @LAN6 "
                                + "		AND "
                                + "	XE.TINHTRANGBD <6 "
                                + "		AND "
                                + "	XE.TINHTRANGGUITIN <6 "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) >= (LO.NGAYBATDAUGUI) "
                                + "		AND "
                                + "	(DATEDIFF(DAY,XE.NGAYMUA, GETDATE())) <= (LO.NGAYKETTHUCGUI)";

            command.Parameters.Add(new SqlParameter("@LAN6", "Lần 6"));
            try
            {
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    TempSoKhung[i] = reader[0].ToString();
                    TempKH[i] = reader[1].ToString();
                    TempSDT[i] = reader[2].ToString();
                    TempHanCuoi[i] = reader[4].ToString();
                    TinNhanLan6 = reader[5].ToString();
                    TempCMND[i] = reader[6].ToString();
                    i++;
                }
                SDTLan6 = PerfectArray(TempSDT);
                TenKHLan6 = PerfectArray(TempKH);
                SoKhungLan6 = PerfectArray(TempSoKhung);
                HanCuoiLan6 = PerfectArray(TempHanCuoi);
                CMNDLan6 = PerfectArray(TempCMND);

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }
        }

        //------------------------------------------------------------------------------------

        public void GuiTinNhan()
        {
            GuiTinNhan1();
            main.ThongBaoTinNhanCanGui.lblSoTNCanGui.Text = "" + SoTinNhanCanGui();
            GuiTinNhan2();
            main.ThongBaoTinNhanCanGui.lblSoTNCanGui.Text = "" + SoTinNhanCanGui();
            GuiTinNhan3();
            main.ThongBaoTinNhanCanGui.lblSoTNCanGui.Text = "" + SoTinNhanCanGui();
            GuiTinNhan4();
            main.ThongBaoTinNhanCanGui.lblSoTNCanGui.Text = "" + SoTinNhanCanGui();
            GuiTinNhan5();
            main.ThongBaoTinNhanCanGui.lblSoTNCanGui.Text = "" + SoTinNhanCanGui();
            GuiTinNhan6();
            main.ThongBaoTinNhanCanGui.lblSoTNCanGui.Text = "" + SoTinNhanCanGui();
        }

        private void GuiTinNhan1()
        {
            //main.
            for (int i = 0; i < SDTLan1.Length; i++)
            {
                if (main.objclsSMS.sendMsg(main.port, SDTLan1[i], TaoTinNhan(SoKhungLan1[i], TenKHLan1[i], HanCuoiLan1[i], TinNhanLan1)))
                {
                    // viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan1[i], TenKHLan1[i], HanCuoiLan1[i], TinNhanLan1),SoKhungLan1[i],"Lần 1",CMNDLan1[i],"Đã gửi");
                    // dua tinh trang gui tin ve 1
                    DoiTinhTrang(SoKhungLan1[i], 1);
                }
                else
                {
                    //viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan1[i], TenKHLan1[i], HanCuoiLan1[i], TinNhanLan1), SoKhungLan1[i], "Lần 1", CMNDLan1[i], "Gửi thất bại");
                }
            }

            Lay_All_Lan1();
        }

        private void GuiTinNhan2()
        {
            //main.
            for (int i = 0; i < SDTLan2.Length; i++)
            {
                if (main.objclsSMS.sendMsg(main.port, SDTLan2[i], TaoTinNhan(SoKhungLan2[i], TenKHLan2[i], HanCuoiLan2[i], TinNhanLan2)))
                {
                    // viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan2[i], TenKHLan2[i], HanCuoiLan2[i], TinNhanLan2), SoKhungLan2[i], "Lần 2", CMNDLan2[i], "Đã gửi");
                    // dua tinh trang gui tin ve 2
                    DoiTinhTrang(SoKhungLan2[i], 2);
                }
                else
                {
                    //viet nhat ki
                    main.QuanLyXe.Visible = true;
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan2[i], TenKHLan2[i], HanCuoiLan2[i], TinNhanLan2), SoKhungLan2[i], "Lần 2", CMNDLan2[i], "Gửi thất bại");
                }
            }

            Lay_All_Lan2();
        }

        private void GuiTinNhan3()
        {
            //main.
            for (int i = 0; i < SDTLan3.Length; i++)
            {
                if (main.objclsSMS.sendMsg(main.port, SDTLan3[i], TaoTinNhan(SoKhungLan3[i], TenKHLan3[i], HanCuoiLan3[i], TinNhanLan3)))
                {
                    // viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan3[i], TenKHLan3[i], HanCuoiLan3[i], TinNhanLan3), SoKhungLan3[i], "Lần 3", CMNDLan3[i], "Đã gửi");
                    // dua tinh trang gui tin ve 3
                    DoiTinhTrang(SoKhungLan3[i], 3);
                }
                else
                {
                    //viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan3[i], TenKHLan3[i], HanCuoiLan3[i], TinNhanLan3), SoKhungLan3[i], "Lần 3", CMNDLan3[i], "Gửi thất bại");
                }
            }

            Lay_All_Lan3();
        }

        private void GuiTinNhan4()
        {
            //main.
            for (int i = 0; i < SDTLan4.Length; i++)
            {
                if (main.objclsSMS.sendMsg(main.port, SDTLan4[i], TaoTinNhan(SoKhungLan4[i], TenKHLan4[i], HanCuoiLan4[i], TinNhanLan4)))
                {
                    // viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan4[i], TenKHLan4[i], HanCuoiLan4[i], TinNhanLan4), SoKhungLan4[i], "Lần 4", CMNDLan4[i], "Đã gửi");
                    // dua tinh trang gui tin ve 4
                    DoiTinhTrang(SoKhungLan4[i], 4);
                }
                else
                {
                    //viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan4[i], TenKHLan4[i], HanCuoiLan4[i], TinNhanLan4), SoKhungLan4[i], "Lần 4", CMNDLan4[i], "Gửi thất bại");
                }
            }

            Lay_All_Lan4();
        }

        private void GuiTinNhan5()
        {
            //main.
            for (int i = 0; i < SDTLan5.Length; i++)
            {
                if (main.objclsSMS.sendMsg(main.port, SDTLan5[i], TaoTinNhan(SoKhungLan5[i], TenKHLan5[i], HanCuoiLan5[i], TinNhanLan5)))
                {
                    // viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan5[i], TenKHLan5[i], HanCuoiLan5[i], TinNhanLan5), SoKhungLan5[i], "Lần 5", CMNDLan5[i], "Đã gửi");
                    // dua tinh trang gui tin ve 5
                    DoiTinhTrang(SoKhungLan5[i], 5);
                }
                else
                {
                    //viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan5[i], TenKHLan5[i], HanCuoiLan5[i], TinNhanLan5), SoKhungLan5[i], "Lần 5", CMNDLan5[i], "Gửi thất bại");
                }
            }

            Lay_All_Lan5();
        }

        private void GuiTinNhan6()
        {
            //main.
            for (int i = 0; i < SDTLan6.Length; i++)
            {
                if (main.objclsSMS.sendMsg(main.port, SDTLan6[i], TaoTinNhan(SoKhungLan6[i], TenKHLan6[i], HanCuoiLan6[i], TinNhanLan6)))
                {
                    // viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan6[i], TenKHLan6[i], HanCuoiLan6[i], TinNhanLan6), SoKhungLan6[i], "Lần 6", CMNDLan6[i], "Đã gửi");
                    // dua tinh trang gui tin ve 6
                    DoiTinhTrang(SoKhungLan6[i], 6);
                }
                else
                {
                    //viet nhat ki
                    LuuNhatKiTinNhan(TaoTinNhan(SoKhungLan6[i], TenKHLan6[i], HanCuoiLan6[i], TinNhanLan6), SoKhungLan6[i], "Lần 6", CMNDLan6[i], "Gửi thất bại");
                }
            }

            Lay_All_Lan6();
        }



        public static string TaoTinNhan(string SoKhung, string TenKH, string HanCuoi, string tinnhan)
        {
            //HanCuoi = HanCuoi.Substring(0, 10);
            DateTime date = DateTime.Parse(HanCuoi);
            HanCuoi = date.ToShortDateString();
            string result = tinnhan;
            result = result.Replace("@", TenKH);
            result = result.Replace("#", SoKhung);
            result = result.Replace("$", HanCuoi);
            return result;
        }

        public void LuuNhatKiTinNhan(string noidung, string sokhung, string loaitin, string cmnd, string tinhtrang)
        {
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand("SP_LUUNHATKI", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Connection = conn;

            command.Parameters.Add(new SqlParameter("@NOIDUNG", noidung));
            command.Parameters.Add(new SqlParameter("@SOKHUNG", sokhung));
            command.Parameters.Add(new SqlParameter("@LOAITN", loaitin));
            command.Parameters.Add(new SqlParameter("@CMND", cmnd));
            command.Parameters.Add(new SqlParameter("@TINHTRANG", tinhtrang));
            try
            {
                conn.Open();
                command.ExecuteNonQuery();


            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }
        }

        public void DoiTinhTrang(string SOKHUNG, int lan)
        {
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "UPDATE XEMAY SET TINHTRANGGUITIN = @lan "
                    + "WHERE SOKHUNG = @SOKHUNG";
            command.Parameters.Add(new SqlParameter("@SOKHUNG", SOKHUNG));
            command.Parameters.Add(new SqlParameter("@lan", lan));
           
            try
            {
                conn.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }

            finally
            {
                command.Dispose();
                conn.Close();
            }
        }

        public int SoTinNhanCanGui()
        {
            int lan1 = 0;
            int lan2 = 0;
            int lan3 = 0;
            int lan4 = 0;
            int lan5 = 0;
            int lan6 = 0;

            if (SDTLan1 == null)
            {
                lan1 = 0;
            }
            else
            {
                lan1 = SDTLan1.Length;
            }

            //--------------------

            if (SDTLan2 == null)
            {
                lan2 = 0;
            }
            else
            {
                lan2 = SDTLan2.Length;
            }

            //--------------------

            if (SDTLan3 == null)
            {
                lan3 = 0;
            }
            else
            {
                lan3 = SDTLan3.Length;
            }

            //--------------------

            if (SDTLan4 == null)
            {
                lan4 = 0;
            }
            else
            {
                lan4 = SDTLan4.Length;
            }

            //--------------------

            if (SDTLan5 == null)
            {
                lan5 = 0;
            }
            else
            {
                lan5 = SDTLan5.Length;
            }

            //--------------------

            if (SDTLan6 == null)
            {
                lan6 = 0;
            }
            else
            {
                lan6 = SDTLan6.Length;
            }

            //--------------------

            return  lan1 + lan2 + lan3 + lan4 + lan5 + lan6;
           
        }

        protected string[] PerfectArray(string[] array)
        {
            int count = 0;
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != null)
                {
                    count++;
                }
            }
            string[] PerfectArray = new string[count];
            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != null)
                {
                    PerfectArray[i] = array[i];
                }
            }
            return PerfectArray;
        }

    }// end class

}// end namespace
